package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.SalaryRuleAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.SalaryFieldBean;
import com.lzx.iteam.bean.SalaryRuleBean;
import com.lzx.iteam.bean.SalaryRuleEditBean;
import com.lzx.iteam.net.*;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.MyListView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2017年 01月 11日
 * Time: 下午2:30
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 设置考勤规定页面
 */
public class SalaryRuleActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.lv_salary_rule)
    private MyListView mListView;


    private SalaryRuleBean mData;
    private SalaryRuleAdapter mAdapter;
    private ArrayList<SalaryRuleEditBean> mRules;
    private HashMap<String,String> mMapRules = new HashMap<>();
    private SalaryRuleBean mRuleData;

    private String mGroupId;

    private final int SET_SALARY_RULE = 3002;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            waitDlgDismiss();
            if (msg.arg1 == 0){
                switch (msg.what){
                    case SET_SALARY_RULE:
                        showShortToast("保存成功");
                        finish();
                        break;

                }
            }else{
                Toast.makeText(mActivity, (String) msg.obj,
                      Toast.LENGTH_LONG).show();
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.salary_rule_layout);
    }

    @Override
    public void initListener() {

        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("考勤规定");
        mTvRight.setText("保存");
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mRuleData = (SalaryRuleBean) intent.getSerializableExtra("salary_rule");
        setRuleData();
    }

    /**
     * 设置列表数据
     */
    private void setRuleData(){
        mRules = new ArrayList<>();
        mRules.add(new SalaryRuleEditBean(1));
        mRules.add(new SalaryRuleEditBean("餐补","考勤天数",mRuleData.getMeal_allowance(),0));
        mRules.add(new SalaryRuleEditBean("出差补贴","出差天数",mRuleData.getBusiness_allowance(),0));
        mRules.add(new SalaryRuleEditBean("加班补贴","加班天数",mRuleData.getOvertime_allowance(),2));

        mRules.add(new SalaryRuleEditBean(2));
        mRules.add(new SalaryRuleEditBean("迟到扣款","迟到次数",mRuleData.getLate_fine(),1));
        mRules.add(new SalaryRuleEditBean("早退扣款","早退次数",mRuleData.getLeave_fine(),1));
        mRules.add(new SalaryRuleEditBean("旷工扣款","旷工天数",mRuleData.getAbsent_fine(),2));

        mAdapter = new SalaryRuleAdapter(mActivity,mRules);
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                getRuleData();
                setSalaryRule();
                break;
        }
    }

    /**
     * 获取列表数据
     */
    private void getRuleData(){
        mMapRules.clear();
        for (int i = 0; i < mListView.getChildCount(); i++) {
            if (mRules.get(i).getType() == 0){
                RelativeLayout layout = (RelativeLayout) mListView.getChildAt(i);// 获得子item的layout
                LinearLayout layout1 = (LinearLayout) layout.findViewById(R.id.ll_rule_item_content);
                RelativeLayout layout2 = (RelativeLayout) layout1.findViewById(R.id.rl_rule_item_edit);
                TextView tv = (TextView) layout1.findViewById(R.id.tv_rule_item_name);
                String name = tv.getText().toString();
                EditText et = (EditText) layout2.findViewById(R.id.ed_rule_item);// 从layout中获得控件,根据其id
                // EditText et = (EditText) layout.getChildAt(1)//或者根据位置
                String money = et.getText().toString().trim();
                if (StringUtil.isEmpty(money)){
                    money = "0";
                }
                mMapRules.put(name,money);
            }
        }
    }

    /**
     * 设置考勤规则
     */
    private void setSalaryRule(){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_MEAL_ALLOWANCE, mMapRules.get("餐补")));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_BUSINESS_ALLOWANCE, mMapRules.get("出差补贴")));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_OVERTIME_ALLOWANCE, mMapRules.get("加班补贴")));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_LATE_FINE, mMapRules.get("迟到扣款")));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_LEAVE_FINE, mMapRules.get("早退扣款")));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_ABSENT_FINE, mMapRules.get("旷工扣款")));
        WithoutObjMsg msg = new WithoutObjMsg(mHandler.obtainMessage(SET_SALARY_RULE));
        msg.mApi = AsynHttpClient.API_SALARY_SET_RULE;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }
}
